package boardgamer.yavalath.client;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import boardgamer.common.client.Drawable;
import boardgamer.common.client.layout.BitmapLayout;
import boardgamer.common.client.layout.HasLayout;
import boardgamer.common.client.layout.InvisibleRect;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.common.client.layout.MutableTextLayout;
import boardgamer.yavalath.client.resources.ImageHandler;

/**
 * @author garysoed
 */
public class Header implements Drawable, HasLayout {

  private static final float TITLE_TOP_RATIO = 0.05f;
  private static final float TITLE_WIDTH_RATIO = 0.8f;
  private static final Paint HEADER_PAINT = new Paint(Paint.ANTI_ALIAS_FLAG);
  static {
    HEADER_PAINT.setColorFilter(new LightingColorFilter(Color.rgb(128, 0, 255), 0));
  }

  private static final float TEXT_MAX_HEIGHT_RATIO = 0.6f;
  private static final float TEXT_LEFT_HEIGHT_RATIO = 0.5f;

  private final BitmapLayout background;
  private final MutableTextLayout textLayout;
  private final LayoutGroup group;

  public Header(String text, Rect canvasRect) {
    this.background = new BitmapLayout(ImageHandler.headerBackground, HEADER_PAINT);

    Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.BLACK);
    textPaint.setTypeface(ImageHandler.headingFont);
    textPaint.setTextSize(RenderHelper.instance().getMaxFittingHeightTextSize(
        this.background.getBoundingRect().height() * TEXT_MAX_HEIGHT_RATIO,
        ImageHandler.headingFont));
    this.textLayout = new MutableTextLayout(
        textPaint,
        background.getBoundingRect().width()
            - background.getBoundingRect().height() * TEXT_LEFT_HEIGHT_RATIO,
        11, // maxCharCount
        LayoutGroup.HorizontalAlign.LEFT,
        ImageHandler.boundSizeRatio.get(ImageHandler.headingFont));
    this.textLayout.setText(text);

    this.group = new LayoutGroup()
        .add(this.background).atCurrentRect()
        .add(this.textLayout)
            .atPosition(
                this.background.getBoundingRect().height() * TEXT_LEFT_HEIGHT_RATIO,
                0)
        .verticalAlign(LayoutGroup.VerticalAlign.CENTER);

    // Layout the header relative to the screen
    LayoutGroup headerGroup = new LayoutGroup()
        .add(this).atCurrentRect();
    float top = canvasRect.height() * TITLE_TOP_RATIO;
    headerGroup.setBoundingRect(new RectF(
        0,
        top,
        canvasRect.width() * TITLE_WIDTH_RATIO,
        top + this.getBoundingRect().height()));
    headerGroup
        .add(new InvisibleRect(canvasRect)).atCurrentRect()
        .horizontalAlign(LayoutGroup.HorizontalAlign.RIGHT);
  }

  @Override
  public void draw(Canvas canvas) {
    background.draw(canvas);
    textLayout.draw(canvas);
  }

  @Override
  public void setBoundingRect(RectF boundingRect) {
    group.setBoundingRect(boundingRect);
  }

  @Override
  public RectF getBoundingRect() {
    return group.getBoundingRect();
  }
}
